import {
	getWebsiteList
} from '../../api/index.js'
const state = {
	websiteList: [],
	novelList: []
}

const mutations = {

	SET_NOVELLIST: (state, data) => {
		data = data.sort(function(a, b) {
			return b.time - a.time
		})

		state.novelList = data
	},
	ADD_NOVELLIST: (state, data) => {
		let list = JSON.parse(JSON.stringify(state.novelList))
		let index = list.map(i => {
			return i.name + "_" + i.author
		}).indexOf(data.name + "_" + data.author)
		if (index > -1) {
			list.splice(index, 1, data)
		} else {
			list.push(data)
		}
		
		list.sort(function(a, b) {
			return b.time - a.time
		})
		state.novelList = list
	},
	SET_WEBSITELIST: (state, data) => {
		state.websiteList = data
	}

}

const actions = {
	getWebsiteList({
		commit
	}, data) {
		return new Promise((resolve, reject) => {
			getWebsiteList().then(res => {
				resolve(res)
				commit('SET_WEBSITELIST', res)
			})
		})

	}
}

export default {
	namespaced: true,
	state,
	mutations,
	actions
}
